package cn.iocoder.yudao.module.expert.service.projectinfo;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.expert.controller.admin.projectinfo.vo.*;
import cn.iocoder.yudao.module.expert.dal.dataobject.projectinfo.ProjectInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.expert.dal.mysql.projectinfo.ProjectInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.expert.enums.ErrorCodeConstants.*;

/**
 * 项目信息 Service 实现类
 *
 * @author 周天贺
 */
@Service
@Validated
public class ProjectInfoServiceImpl implements ProjectInfoService {

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Override
    public Long createProjectInfo(ProjectInfoSaveReqVO createReqVO) {
        // 插入
        ProjectInfoDO projectInfo = BeanUtils.toBean(createReqVO, ProjectInfoDO.class);
        projectInfo.setExtractBatchNum(0);
        projectInfoMapper.insert(projectInfo);
        // 返回
        return projectInfo.getId();
    }

    @Override
    public void updateProjectInfo(ProjectInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectInfoExists(updateReqVO.getId());
        // 更新
        ProjectInfoDO updateObj = BeanUtils.toBean(updateReqVO, ProjectInfoDO.class);
        projectInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectInfo(Long id) {
        // 校验存在
        validateProjectInfoExists(id);
        // 删除
        projectInfoMapper.deleteById(id);
    }

    private void validateProjectInfoExists(Long id) {
        if (projectInfoMapper.selectById(id) == null) {
            throw exception(PROJECT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public ProjectInfoDO getProjectInfo(Long id) {
        return projectInfoMapper.selectById(id);
    }

    @Override
    public PageResult<ProjectInfoDO> getProjectInfoPage(ProjectInfoPageReqVO pageReqVO) {
        return projectInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public boolean importProjectInfoList(List<ProjectInfoImportExcelVO> list) {
        List<ProjectInfoDO> infoDOS= BeanUtils.toBean(list, ProjectInfoDO.class);
        return projectInfoMapper.insertBatch(infoDOS);
    }
}